package com.example.readbook.UI.Act

import android.view.Menu
import android.view.MenuItem
import com.example.readbook.R
import com.example.readbook.Sql.JournalUtils
import com.example.readbook.Sql.SqliteUtils
import com.example.readbook.UI.BaseUI.BaseActivity
import com.example.readbook.Utils.Utils
import kotlinx.android.synthetic.main.act_write_journal_entry.*

/**
 * Package    : com.example.readbook.UI.Act
 * ClassName  : WriteJournalEntryActivity
 * Description: $写可以选择书籍读后感 的界面
 * Date       : 2019/3/26 13:20
 */
class WriteJournalEntryActivity : BaseActivity() {
    var chooseBookName = ""

    override fun getLayoutResID(): Int {
        return R.layout.act_write_journal_entry
    }

    override fun initView() {
        title = "写读后感"
        val readAllBookName = SqliteUtils.getInstence(this@WriteJournalEntryActivity).readAllBookName()
        Act_Write_Spinn.setItems(readAllBookName)
        Act_Write_Spinn.selectedIndex = 0
        chooseBookName = readAllBookName[0]
        Act_Write_Spinn.setOnItemSelectedListener { view, position, id, item ->
            chooseBookName = item as String
        }
    }

    override fun initListener() {

    }

    override fun initData() {

    }

    //右上角菜单点击事件监听
    override fun onOptionsItemSelected(item: MenuItem?): Boolean {
        if (item?.itemId == R.id.save) {
            if (Act_Write_Edit.text.toString().isEmpty()) {
                Utils.showToast("读后感还没有写哦～")
                return true
            }
            JournalUtils.getInstence(this@WriteJournalEntryActivity)
                    .addJournal(chooseBookName, Act_Write_Edit.text.toString())
            mActivity.finish()
            return true
        }
        return super.onOptionsItemSelected(item)
    }

    //加载右上角菜单
    override fun onCreateOptionsMenu(menu: Menu?): Boolean {
        menuInflater.inflate(R.menu.write, menu)
        return super.onCreateOptionsMenu(menu)
    }
}
